﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ShopOnline.Models.Entities;

namespace ShopOnline.Models.DAL
{
    public class DAL_ProductCatelogy
    {
        private ShopOnline.Models.ShopOnlineDBDataContext SC_DB;
        private DAL_Product productRepository;
        public DAL_ProductCatelogy()
        {
            SC_DB = new ShopOnlineDBDataContext();
            productRepository = new DAL_Product();
        }


        public List<ProductCatelogy> getListProductCatelogy()
        {
            return SC_DB.ProductCatelogies.ToList();
        }

        public List<ProductCatelogy> getListProductCatelogyIsShow()
        {
            return (from c in SC_DB.ProductCatelogies where c.show == true select c).ToList();
        }



        public ProductCatelogy getProductCatelogyById(int catelogyId)
        {
            return (from c in SC_DB.ProductCatelogies where c.catelogyId == catelogyId select c).FirstOrDefault();
        }

        public int CreateProductCatlogy(ProductCatelogy cate)
        {
            int result = 0;
            try
            {
                SC_DB.ProductCatelogies.InsertOnSubmit(cate);
                SC_DB.SubmitChanges();
                result = 1;
            }
            catch
            {

            }
            return result;
        }

        public int EditProductCatlogy(ProductCatelogy cate)
        {
            int result = 0;
            try
            {
                var catelogy = (from c in SC_DB.ProductCatelogies where c.catelogyId == cate.catelogyId select c).FirstOrDefault();
                catelogy.description = cate.description;
                catelogy.show = cate.show;
                catelogy.catelogyName = cate.catelogyName;
                catelogy.logo = cate.logo;
                catelogy.piority = cate.piority;
                SC_DB.SubmitChanges();
                result = 1;
            }
            catch
            {

            }
            return result;
        }

        public int DeleteProductCatelogy(ProductCatelogy cate)
        {
            int result = 0;
            try
            {
                var catelogy = (from c in SC_DB.ProductCatelogies where c.catelogyId == cate.catelogyId select c).FirstOrDefault();
                SC_DB.ProductCatelogies.DeleteOnSubmit(catelogy);
                SC_DB.SubmitChanges();
                result = 1;
            }
            catch
            {

            }
            return result;
        }
        public List<Product> GetListProductByCatelogyId(int id)
        {
            var list = (from a in SC_DB.Products where a.productId == id && a.show == true select a).ToList();
            return list;
        }
        public List<ProductView> getListProductView()
        {
            List<ProductView> listProductView = new List<ProductView>();
            List<ProductCatelogy> listProCate = getListProductCatelogy();
            foreach (var item in listProCate)
            {
                ProductView proView = new ProductView();
                proView.ProductCate = item;
                proView.ListProduct = productRepository.getListProductByCateId(item.catelogyId);
                listProductView.Add(proView);
            }
            return listProductView;
        }

    }
}